var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var ad = require("./ad");
var in_task = require("./inTask");
var point_map = require("./pointMap");
var read = {
  go_page_from_home() {
    var logger = utils.create_logger("read go_page_from_home: ");
    if (ocr_utils.screen_exists_text_all(["我的", "视频"], true)) {
      logger("SUCCESS");
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      var swipe_down_times = utils.get_random_interval(1, 0, 1, 1);
      logger("来到首页，下面开始向下滑动-" + swipe_down_times + "-次");
      while (swipe_down_times > 0) {
        utils.swipe(1 / 2, 7 / 10, 1 / 2, 2 / 10);
        sleep(utils.get_random_interval(2, 0, 1, 1000));
        swipe_down_times = swipe_down_times - 1;
        logger("还需要向下滑动-" + swipe_down_times + "-次");
      }
      this.init();
    } else {
      logger("NOT FOUND");
    }
  },
  init() {
    var logger = utils.create_logger("read init: ");
    var content = ["小时前", "天前", "周前", "前天", "月前"];
    var ocr_text_results = ocr_utils.find_text_all(content, false);
    var length = ocr_text_results.length;
    if (length > 0) {
      logger("找到-" + length + "个-" + content.join("|||") + "-");
      for (var i = 0; i < length; i++) {
        if (i == 1) {
          break;
        }
        var { result } = ocr_text_results[i];
        this.run(i, result);
      }
      this.after();
    } else {
      logger("没找到-" + content.join("|||") + "-");
      this.after();
    }
  },
  run(i, result) {
    var logger = utils.create_logger("read run: ");
    logger(
      " 第-" + (i + 1) + "-篇文章或视频，找到的内容是-" + result.text + "-"
    );
    utils.click_ocr_text(result);
    sleep(utils.get_random_interval(3, 0, 2, 1000));
    if (ocr_utils.screen_exists_text_one(["全屏", "分享"], false)) {
      this.watch_video(i, result);
    } else {
      this.read(i, result);
    }
  },
  watch_video(i, result) {
    var logger = utils.create_logger("read watch_video: ");
    var interval = utils.get_random_interval(35, 0, 5, 1000);
    logger("找到-全屏或者分享-，开始看-" + interval / 1000 + "-秒视频");
    if (utils.click_text_starts_with("点击领取")) {
      sleep(utils.get_random_interval(2, 0, 1, 1000));
      logger("找到-点击领取-");
      sleep(utils.get_random_interval(2, 0, 1, 1000));

      var ocr_text = ocr_utils.screen_exists_text_one("看视频再", false);
      if (ocr_text) {
        utils.click_ocr_text(ocr_text);
        logger("找到-看视频再-");
        ad.init(
          function () {
            var result = utils.exists_text_starts_with("领取成功");
            if (result) {
              logger("找到-领取成功-");
            }
            return result;
          },
          function () {
            if (utils.exists_text_starts_with("恭喜你获得")) {
              logger("找到-恭喜你获得-");
              return true;
            }
            var ocr_texts = ["回复", "种树", "开心收下", "最多再领"];
            var ocr_text = ocr_utils.screen_exists_text_one(ocr_texts, true);
            if (ocr_text) {
              logger("找到-" + ocr_texts.join(" 或 ") + "-");
              return true;
            }
          },
          function () {
            return ocr_utils.screen_exists_text_all(["首页", "发现"], false);
          },
          ["看视频再得"]
        );
      }
      var ocr_text = ocr_utils.screen_exists_text_one("开心收下", false);
      if (ocr_text) {
        utils.click_ocr_text(ocr_text);
        logger("找到-开心收下-");
      }
    }
    sleep(interval);
    back();
    sleep(utils.get_random_interval(2, 0, 1, 1000));
  },
  read(i, result) {
    var logger = utils.create_logger("task_read read: ");
    logger("开始阅读");
    sleep(utils.get_random_interval(2, 0, 1, 1));
    var max_read_times = utils.get_random_interval(30, 0, 5, 1);
    var has_read_times = 0;
    logger("将滑动-" + max_read_times + "-次");
    while (has_read_times < max_read_times) {
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      swipe(
        (device.width / 2) * utils.get_swipe_times(92, 8),
        (device.height / 10) * 6 * utils.get_swipe_times(92, 8),
        (device.width / 2) * utils.get_swipe_times(92, 8),
        (device.height / 10) * 3 * utils.get_swipe_times(92, 8),
        utils.get_random_interval(300, 0, 700, 1)
      );
      has_read_times = has_read_times + 1;
      if (has_read_times > 5) {
        logger("已滑动-" + has_read_times + "-次，下面开始找看文章的奖励");
        var find_result = this.find_read_award(false);
        if (find_result) {
          sleep(utils.get_random_interval(2, 0, 2, 1000));
          break;
        }
        var is_over_contents = ["全部评论", "抢沙发", "回复"];
        var ocr_text = ocr_utils.screen_exists_text_one(
          is_over_contents,
          false
        );
        if (ocr_text) {
          logger(
            "找到-" + is_over_contents.join(" 或 ") + "-，即将结束本次阅读"
          );
          sleep(utils.get_random_interval(2, 0, 2, 1000));
          break;
        }
      } else {
        logger("已滑动-" + has_read_times + "-次");
      }
    }
    back();
    sleep(utils.get_random_interval(2, 0, 2, 1000));
  },
  find_read_award(log_ocr_texts) {
    var logger = utils.create_logger("read find_read_award: ");
    var ocr_text = ocr_utils.screen_exists_text_one(
      "阅读惊喜奖励",
      log_ocr_texts
    );
    if (ocr_text) {
      logger("找到-阅读惊喜奖励-");
      utils.click_ocr_text(ocr_text);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      var ocr_text = ocr_utils.screen_exists_text_one("开心收下", false);
      if (ocr_text) {
        utils.click_ocr_text(ocr_text);
        logger("找到-开心收下-");
        return true;
      }
      var ocr_text = ocr_utils.screen_exists_text_one("看视频领", false);
      if (ocr_text) {
        utils.click_ocr_text(ocr_text);
        logger("找到-看视频领-");
        ad.init(
          function () {
            return utils.exists_text_starts_with("领取成功");
          },
          function () {
            if (utils.exists_text_starts_with("恭喜你获得")) {
              logger("找到-恭喜你获得-");
              return true;
            }
            var ocr_texts = ["回复", "种树", "开心收下"];
            var ocr_text = ocr_utils.screen_exists_text_one(ocr_texts, false);
            if (ocr_text) {
              logger("找到-" + ocr_texts.join(" 或 ") + "-");
              return true;
            }
          },
          function () {
            return ocr_utils.screen_exists_text_all(["首页", "发现"], false);
          },
          ["看视频再得"]
        );
        if (ad.in_task) {
          back();
          sleep(utils.get_random_interval(2, 0, 1, 1000));
        }
        return true;
      }
      return true;
    }
    logger("没找到-阅读惊喜奖励-");
    return false;
  },
  after() {
    console.log("read after");
    this.next();
  },
  next() {
    console.log("read next");
  },
};
module.exports = read;
